//
//  RecommendCycleView.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/23.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

private let kItemId:String = "kItemId"

class RecommendCycleView: UIView {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pageControl: UIPageControl!
    private var timer:Timer?
    /// 轮播数据
    var cycleDatas:[CycleModel]?{
        didSet{
            pageControl.numberOfPages = self.cycleDatas!.count
            collectionView.reloadData()
            let indexPath:NSIndexPath = NSIndexPath(item: (self.cycleDatas?.count ?? 0) * 1000, section: 0)
            collectionView.scrollToItem(at: indexPath as IndexPath, at: .left, animated: false)
            addTimer()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 不随着父控件的拉伸而拉伸
        autoresizingMask = UIView.AutoresizingMask()
        collectionView.register(CycleCollectionViewCell.self, forCellWithReuseIdentifier: kItemId)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = collectionView.bounds.size
    }
    
}

extension RecommendCycleView{
    class func cycleView() -> RecommendCycleView{
        let cycleView:RecommendCycleView = Bundle.main.loadNibNamed("RecommendCycleView", owner: nil, options: nil)?.first as! RecommendCycleView
        return cycleView
    }
}
// MARK: 遵守协议
extension RecommendCycleView:UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (cycleDatas?.count ?? 0) * 10000
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kItemId, for: indexPath) as! CycleCollectionViewCell
        cell.backgroundColor = UIColor.white
        let item:Int = indexPath.item % cycleDatas!.count
        let cycleModel = cycleDatas![item]
        cell.cycleModel = cycleModel
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int((scrollView.contentOffset.x + scrollView.bounds.width * 0.5) / scrollView.bounds.width) % cycleDatas!.count
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeTimer()
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        addTimer()
    }
    
}

// MARK:定时器相关
extension RecommendCycleView{
    private func addTimer(){
        timer = Timer(timeInterval: 3, target: self, selector: #selector(scrollToNext), userInfo: nil, repeats: true)
        RunLoop.main.add(timer!, forMode: .common)
    }
    @objc private func scrollToNext(){
        let offsetX = collectionView.contentOffset.x + collectionView.bounds.width
        collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    }
    private func removeTimer() {
        timer?.invalidate()
        timer = nil
    }
}
